package com.css.apps.base.user.action.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.user.model.SUser;
import com.css.core.configuration.ConfigurationManager;
import com.css.core.configuration.Environment;
import com.css.core.model.tree.Tree;
import com.css.core.model.tree.TreeCache;
import com.css.core.model.tree.TreeNode;
import com.css.db.page.Page;
import com.css.db.query.QueryCache;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class DirUserAdmin extends AdminAction {
	
	private static Log log = LogFactory.getLog(DirUserAdmin.class);
	private String name, openFlag, orgId, includeFlag = "1", tree, activeStatus;
	private String userType;
	private Page page;

	public DirUserAdmin() {
		page = new Page();
		page.setCountField("a.uuid");
		page.setOrderString("a.orderNum");
	}

	@SuppressWarnings("unchecked")
	@Override
	protected String adminGo() {
		try {
			QueryCache qc = new QueryCache("select a.uuid from SUser a, SUserOrg b " + getWhere() + getOrder());
			setWhere(qc);
			page = qc.page(page);
			List<SUser> users = QueryCache.idToObj(SUser.class, page.getResults());
			for (SUser user : users) {
				if (StringHelper.isEmpty(orgId)) {
					orgId = user.getOrganId();
				}
				user.setOrgId(orgId);
			}
			page.setResults(users);
			return SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			setMessage(Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getOrder() {
		return StringHelper.isNotEmpty(page.getOrderByString()) ? page.getOrderByString() : "order by a.issueDate desc";
	}

	public String getWhere() {
		StringBuffer sb = new StringBuffer("where a.uuid = b.userId and b.deptId in(:orgId) and a.delFlag='" + Environment.UN_DELETE_STATE + "'");
		// StringBuffer sb = new StringBuffer("where a.delFlag='" +
		// Environment.UN_DELETE_STATE + "'");
		// run模式下只能显示普通用户
		if (!ConfigurationManager.isAdminMode()) {
			sb.append(" and a.userType = '1' ");
		} else if (StringHelper.isNotEmpty(userType)) {
			sb.append(" and a.userType =  '" + userType + "' ");
		}
		// if(StringHelper.isNotEmpty(orgId))
		// sb.append(" and a.orgId in(:orgId) ");
		if (StringHelper.isNotEmpty(name))
			sb.append(" and (a.loginName like :name or a.realName like :name) ");
		// if(StringHelper.isNotEmpty(loginName))
		// sb.append(" and a.loginName like :loginName ");
		// if(StringHelper.isNotEmpty(realName))
		// sb.append(" and a.realName like :realName ");
		if (StringHelper.isNotEmpty(openFlag))
			sb.append(" and a.openFlag = :openFlag ");
		if (StringHelper.isNotEmpty(activeStatus))
			sb.append(" and a.activeStatus = :activeStatus ");
		return sb.toString();
	}

	public void setWhere(QueryCache qc) {
		Tree tree = new TreeCache(SOrg.class);
		if (StringHelper.isNotEmpty(orgId)) {
			if ("2".equals(includeFlag)) {
				List<Serializable> orgIds = tree.getSerializeIdList(tree.getTreeNode(orgId).getAllChildren());
				orgIds.add(orgId);
				qc.setParameter("orgId", orgIds);
			} else {
				qc.setParameter("orgId", orgId);
			}
		} else {
			TreeNode rootNode = tree.getRoot().get(0);
			if ("2".equals(includeFlag)) {
				List<String> orgIds = new ArrayList<String>();
				List<TreeNode> treeNodes = tree.getTree();
				for (TreeNode node : treeNodes) {
					orgIds.add(node.getNodeId());
				}
				orgIds.add(rootNode.getNodeId());
				qc.setParameter("orgId", orgIds);
			} else {
				List<String> orgIds = new ArrayList<String>();
				orgIds.add(rootNode.getNodeId());
				qc.setParameter("orgId", orgIds);
			}
		}
		if (StringHelper.isNotEmpty(name))
			qc.setParameter("name", "%" + name.trim() + "%");
		if (StringHelper.isNotEmpty(openFlag))
			qc.setParameter("openFlag", openFlag.trim());
		if (StringHelper.isNotEmpty(activeStatus))
			qc.setParameter("activeStatus", activeStatus.trim());

	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getOpenFlag() {
		return openFlag;
	}

	public void setOpenFlag(String openFlag) {
		this.openFlag = openFlag;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public String getIncludeFlag() {
		return includeFlag;
	}

	public void setIncludeFlag(String includeFlag) {
		this.includeFlag = includeFlag;
	}

	public String getTree() {
		return tree;
	}

	public void setTree(String tree) {
		this.tree = tree;
	}

	public String getActiveStatus() {
		return activeStatus;
	}

	public void setActiveStatus(String activeStatus) {
		this.activeStatus = activeStatus;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

}
